package mr.aurelien.blackboard;

import mr.aurelien.blackboard.view.BlackboardUI;
import mr.aurelien.blackboard.view.Pen;
import mr.aurelien.sample.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {
	private BlackboardUI board;
	private ClipboardManager clipboard;
	private Pen pen;
	private SeekBar penBar;
	private Button color1;
	private Button color2;
	private Button color3;
	private Button color4;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        setContentView(R.layout.activity_main);
        this.board = (BlackboardUI) this.findViewById(R.id.board);
        this.pen = (Pen) this.findViewById(R.id.pen);
        this.color1 = (Button) this.findViewById(R.id.btColor1);
        color1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//onPenColorChange(v.getBackground().);
				ColorDrawable buttonColor = (ColorDrawable) v.getBackground();
				onPenColorChange(buttonColor.getColor());
			}
		});
        this.color2 = (Button) this.findViewById(R.id.btColor2);
        color2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//onPenColorChange(v.getBackground().);
				ColorDrawable buttonColor = (ColorDrawable) v.getBackground();
				onPenColorChange(buttonColor.getColor());
			}
		});
        this.color3 = (Button) this.findViewById(R.id.btColor3);
        color3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//onPenColorChange(v.getBackground().);
				ColorDrawable buttonColor = (ColorDrawable) v.getBackground();
				onPenColorChange(buttonColor.getColor());
			}
		});
        this.color4 = (Button) this.findViewById(R.id.btColor4);
        color4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//onPenColorChange(v.getBackground().);
				ColorDrawable buttonColor = (ColorDrawable) v.getBackground();
				onPenColorChange(buttonColor.getColor());
			}
		});
        this.penBar = (SeekBar) this.findViewById(R.id.btPenSize);
        penBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        	@Override
		    public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
		        // TODO Auto-generated method stub
        		onPenSizeChange(progress);
		    }

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
		
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	ClipData clip;
    	// Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_quit:
            this.finish();
            return true;
        case R.id.menu_clean:
            board.clean();
            return true;
        case R.id.menu_copy: 
            clip = android.content.ClipData.newPlainText("Aln",board.save());
            clipboard.setPrimaryClip(clip);
            return true;
        case R.id.menu_draw:
            board.clean();
            board.load(clipboard.getPrimaryClip().getItemAt(0).getText().toString());
            return true;
        default:
            return super.onOptionsItemSelected(item); // what? 
        }
    }
    
	@Override
	public void onResume() {
		super.onResume();
		//board.redrawNeeded();
	}
	
	private void onPenColorChange(int color)
	{
		board.penColor = color;
		pen.SetColor(color);
	}
	
	private void onPenSizeChange(int size)
	{
		board.penSize = pen.SetSize(size);
	}
	
}
